package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
import service.OrderService;

import java.util.Map;

/**
 * ClassName: OrderController
 * Description:
 * date: 2019/11/26 13:11
 * Author Airson
 * Since JDK 1.8
 */
@RequestMapping("/order")
@RestController
@Slf4j
public class OrderController {

    @Reference
    OrderService orderService;
    @Autowired
    JedisPool jedisPool;

    //提交预约表单数据,包含短信验证码验证
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map<String, String> map) {
        log.debug("map:{}",map);
        //获取表单中的电话
        String telephone = map.get("telephone");
        //获取表单中的验证码
        String validateCodeInParam = map.get("validateCode");
        //获取redis中的验证码
        String validateCodeInRedis = jedisPool.getResource().get(RedisConst.SENDTYPE_ORDER + "-" + telephone);

        if (!(validateCodeInRedis != null && validateCodeInRedis.equals(validateCodeInParam))) {
            //验证失败
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }

        //设置预约类型
        map.put("orderType", Order.ORDERTYPE_WEIXIN);

        //添加预约信息到t_order
        Result result = orderService.add(map);;
//        try {
//            result = orderService.add(map);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//
//        }
//        if (result.isFlag()){
//            //添加预约信息成功,给客户发短信通知
//            System.out.println("模拟添加成功");
//           // SMSUtils.sendShortMessage(telephone,"123111");
//        }

        log.debug("result返回值打印--------------------:{}",result);
        return result;
    }
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            orderService.findById(id);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
